<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\PhotoResource;
use App\Models\Photo;
use App\Models\Plan;
use App\Services\UploadService;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;

class PhotoController extends Controller
{
    /**
     * 上传原图
     * @param Request $request
     * @param Plan $plan
     * @param UploadService $service
     * @return mixed
     * @throws AuthorizationException
     */
    public function store(Request $request, Plan $plan, UploadService $service)
    {
        $this->authorize('update', $plan); // 用户资源授权策略

        $data = $service->upload($request);

        $photo = $plan->photoes()->create([
            'path' => $data['path'],
            'user_id' => $this->id()
        ]);

        return $this->success(new PhotoResource($photo));
    }

    /**
     * 上传精修图
     * @param Request $request
     * @param Plan $plan
     * @param Photo $photo
     * @param UploadService $service
     * @return mixed
     * @throws AuthorizationException
     */
    public function retouch(Request $request, Plan $plan, Photo $photo, UploadService $service)
    {
        if ($photo->plan_id !== $plan->id) {
            return $this->failed('参数错误');
        }
        $this->authorize('update', $plan); // 用户资源授权策略

        $data = $service->upload($request);
        $photo->retouch_path = $data['path'];
        $photo->save();

        return $this->success(new PhotoResource($photo));
    }

    /**
     * 删除原图
     * @param Plan $plan
     * @param Photo $photo
     * @return mixed
     * @throws AuthorizationException
     * @throws \Exception
     */
    public function destroy(Plan $plan, Photo $photo)
    {
        if ($photo->plan_id !== $plan->id) {
            return $this->failed('参数错误');
        }
        $this->authorize('update', $plan); // 用户资源授权策略
        $photo->delete();

        return $this->success('拍摄原图删除成功');
    }

    /**
     * 精修评论
     * @param Request $request
     * @param Plan $plan
     * @param Photo $photo
     * @return mixed
     * @throws AuthorizationException
     */
    public function comments(Request $request, Plan $plan, Photo $photo)
    {
        if ($photo->plan_id !== $plan->id) {
            return $this->failed('参数错误');
        }
        $this->authorize('view', $plan); // 用户资源授权策略

        $request->validate([
            'description' => 'required|max:255'
        ], [
            'description.required' => '评论内容不能为空',
            'description.max' => '评论内容最多为255个字符',
        ]);

        if (empty($photo->retouch_path)) {
            return $this->failed('请上传精修图之后再对其评论');
        }

        $photo->fill($request->only('description'));
        $photo->save();

        return $this->success(new PhotoResource($photo));
    }

    /**
     * 加入/取消精修
     * @param Plan $plan
     * @param Photo $photo
     * @return mixed
     * @throws AuthorizationException
     */
    public function toggle(Plan $plan, Photo $photo)
    {
        $this->authorize('view', $plan);

        if ($photo->plan_id !== $plan->id) {
            return $this->failed('参数错误');
        }
        $photo->retouch = !$photo->retouch;
        $photo->save();

        return $this->success('操作成功');
    }

    /**
     * 移除精修图
     * @param Plan $plan
     * @param Photo $photo
     * @return mixed
     * @throws AuthorizationException
     */
    public function update(Plan $plan, Photo $photo)
    {
        if ($photo->plan_id !== $plan->id) {
            return $this->failed('参数错误');
        }
        $this->authorize('view', $plan); // 用户资源授权策略

        $photo->retouch_path = '';
        $photo->save();

        return $this->success(new PhotoResource($photo));
    }
}
